package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.excption.ArchiveException;
import zhongbo.java.air.dictionary.excption.ExceptionCode;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.AlertZone;
import zhongbo.java.air.dictionary.service.IAlertZoneService;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AlertZoneServiceImpl implements IAlertZoneService {
    @Autowired
    private AlertZoneMapper alertZoneMapper;

    @Override
    public AlertZone add(AlertZone Zone) {
        validAlertZone(Zone);

        Zone.setCreateTime(new Date());
        Zone.setUpdateTime(new Date());
        int insert = alertZoneMapper.insert(Zone);
        if (insert > 0) {
            return Zone;
        }
        throw new RuntimeException("区域添加失败");
    }

    private void validAlertZone(AlertZone zone) {
        if (zone == null) {
            throw new RuntimeException("区域信息不能为空");
        }
        List<AlertZone> alertZones = alertZoneMapper.selectList(null);
        String code = zone.getZoneCode();
        if (code.length() > 20) {
            throw new RuntimeException("ICAO不能超过20个字段");
        }
        for (AlertZone alertZone : alertZones) {

            if (code.equals(alertZone.getZoneCode())){
                throw new ArchiveException(ExceptionCode.UNIQUE_FIELD);
            }
        }
//        AZStatus status = zone.getZoneType();
//        if (status != AZStatus.RESTRICTED && status != AZStatus.TEMPORARY && status != AZStatus.NO_FLY){
//            throw new RuntimeException("只能是枚举值：NO_FLY-禁飞区, RESTRICTED-限制区, TEMPORARY-临时区");
//        }
    }

    @Override
    public String delete(String id) {

        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        int delete = alertZoneMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new ArchiveException(ExceptionCode.CANNOT_BE_DELETE);
    }

    @Override
    public AlertZone edit(AlertZone Zone1) {
        if (Zone1 == null) {
            throw new RuntimeException("区域信息不能为空");
        }
        String id = Zone1.getId();
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        AlertZone Zone = alertZoneMapper.selectById(id);
        if (Zone == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_MODIFIED);
        }
        int update = alertZoneMapper.updateById(Zone1);
        Zone1.setUpdateTime(new Date());
        if (update > 0) {
            return Zone1;
        }
        throw new RuntimeException("修改区域失败");
    }

    @Override
    public AlertZone findOne(String id) {
        if (StrUtil.isBlank(id)) {
            throw new ArchiveException(ExceptionCode.NOT_BE_EMPTY);
        }
        AlertZone Zone = alertZoneMapper.selectById(id);
        if (Zone == null) {
            throw new ArchiveException(ExceptionCode.CANNOT_BE_QUERY);
        } else {
            return Zone;
        }
    }

    @Override
    public PageVo<AlertZone> findPage(String keyword,String fields,Integer pageNumber, Integer pageSize, String oderByField, Boolean oderByAsc) {
        QueryWrapper<AlertZone> queryWrapper = new QueryWrapper<>();
        //支持指定字段模糊搜索Keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }

        }
        //检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            //检测是正序还是倒序
            boolean isOderByAsc = Boolean.TRUE.equals(oderByAsc);
            if (isOderByAsc) {
                //正
                queryWrapper.orderByAsc(oderByField);
            } else {
                //倒序
                queryWrapper.orderByAsc(oderByField);
            }
        }
        Page<AlertZone> pageParam = new Page<>(pageNumber, pageSize);
        Page<AlertZone> airZonePage = alertZoneMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airZonePage);
    }
}
